<?php 

// +---------------------------------------------+
// |     Copyright  2010 - 2018 InterPhoto       |
// |     http://www.weentech.com                 |
// |     This file may not be redistributed.     |
// +---------------------------------------------+


if(false == strstr($_SERVER['HTTP_REFERER'],$_SERVER['HTTP_HOST'])) { 
	exit(); 
} 

include('includes/InterPhoto.Core.php');


$getfiles = explode('/', base64_decode(ForceIncomingString('file')));

if(!$getfiles OR sizeof($getfiles) < 3){
	die('File not found!');
}

$imageid = ForceInt($getfiles[1]);

$checkimage = $DB->query_first("SELECT i.watermark, i.sale, c.watermarkpos FROM " . TABLE_PREFIX . "images i LEFT JOIN  " . TABLE_PREFIX . "categories c ON (c.categoryid = i.categoryid) WHERE (i.usergroupids = 'all' OR i.usergroupids LIKE '%(".$userinfo['groupid'].")%') AND i.actived = 1 AND c.actived = 1 AND i.imageid = $imageid ");



if ($checkimage AND $userinfo['allowview']) {
	$imagesize = "760x760";
	$image_path = BASEPATH.'MyWebsiteImages/'.ForceDir($getfiles[0]).'_'.WEBSITE_KEY.'/'.$imagesize.'/'.ForceDir($getfiles[2]);

	header("Expires: " . gmdate("D, d M Y H:i:s", time() + (30 * 86400)) . " GMT");
	header('Content-Type: image/jpeg');

	if (file_exists($image_path)) {
		if($checkimage['watermark'] OR $checkimage['sale']){
			$image_rsrc = imagecreatefromjpeg($image_path);

			$im = T_PATH . 'images/watermark.png';
			if (!file_exists($im)) {
				$im = BASEPATH . 'includes/images/watermark.png';
			}
			$image_id = imagecreatefrompng($im);
			$im_X = ImageSX($image_id);
			$im_Y = ImageSY($image_id);
			$bg_X = ImageSX($image_rsrc);
			$bg_Y = ImageSY($image_rsrc);

			switch ($checkimage['watermarkpos']) {
				case 1: // Top left
					$offset_X	= 10;
					$offset_Y	= 10;
				break;
				
				case 2: // Top right
					$offset_X	= $bg_X - (10 + $im_X);
					$offset_Y	= 10;
				break;	
				
				case 3: // Bottom left
					$offset_X	= 10;
					$offset_Y	= $bg_Y - (10 + $im_Y);
				break;
				
				case 4: // Bottom right
					$offset_X	= $bg_X - (10 + $im_X);
					$offset_Y	= $bg_Y - (10 + $im_Y);
				break;

				default: // Center
					$offset_X	= (($bg_X/2)-($im_X/2));
					$offset_Y	= (($bg_Y/2)-($im_Y/2));
				break;
			}

			imagealphablending($image_rsrc, true);
			imagecopy($image_rsrc, $image_id, $offset_X, $offset_Y, 0, 0, $im_X, $im_Y);
			@imagedestroy($image_id);

			// Write an image
			imagejpeg($image_rsrc, '', 85);
			@imagedestroy($image_rsrc);

		}else{
			echo fread(fopen($image_path, 'r'), filesize($image_path));
			@fclose($image_path);
		}
	} else {
		$image_path = T_PATH . 'images/' . $imagesize . '_missing.gif';
		if (!file_exists($image_path)) {
			$image_path = BASEPATH . 'includes/images/' . $imagesize . '_missing.gif';
		}
		echo fread(fopen($image_path, 'r'), filesize($image_path));
		@fclose($image_path);
	}

}else{
	die('File not found!');
}


?>